/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.saville.serdes;

import java.util.concurrent.atomic.*;
import java.util.Vector;


/**
 * A serdesable Vector<String>.
 * 
 * TODO: I don't allow strgs to be null to make things
 * using this easy but maybe I should?
 */
public class VectorStringSer implements ISerdes<VectorStringSer> {
    public static final int SERDES_ID = 5;
    public static final String SERDES_ID_STRING = "5";

    public Vector<String> strgs;

    public VectorStringSer() {
        strgs = new Vector<String>();
    }
    
    public VectorStringSer(Vector<String> vs) {
        if (vs == null) {
            strgs = new Vector<String>();
        } else {
            strgs = vs;
        }
    }

    public int serdesId() {
        return SERDES_ID;
    }

    public String serdesIdString() {
        return SERDES_ID_STRING;
    }

    public ISerdes<VectorStringSer> make() {
        return new VectorStringSer();
    }

    public void serialize(SerdesBinary serdesBinary) {
        serdesBinary.writeInt(serdesId());
        serdesBinary.writeInt(strgs.size());
        for (int i = 0; i < strgs.size(); i++) {
            serdesBinary.writeString(strgs.get(i));
        }
    }

    public VectorStringSer deserialize(SerdesBinary serdesBinary) {
        int len = serdesBinary.readInt();
        for (int i = 0; i < len; i++) {
            strgs.add(serdesBinary.readString());
        }
        return this;
    }
    
    public void serialize(SerdesJson serdesJson) {
        try {
            serdesJson.startObject();
            serdesJson.writeKey(serdesIdString());
            serdesJson.startArray();
            for (int i = 0; i < strgs.size(); i++) {
                serdesJson.writeString(strgs.get(i));
            }
            serdesJson.endArray();
            serdesJson.endObject();
        } catch (Exception e) {
            throw new RuntimeException("StringSer#serialize json error");
        }
    }

    public VectorStringSer deserialize(SerdesJson serdesJson) {
        try {
            serdesJson.startArrayRead();
            while (serdesJson.isEndArrayRead() == false) {
                strgs.add(serdesJson.readString());
            }
            serdesJson.endArrayRead();
        } catch (Exception e) {
            throw new RuntimeException("VectorStringSer#deserialize json error");
        }
        return this;
    }
    
    /*
     * This code should be present in all classes
     * that implement ISerdes
     */
    {
        if (registered.compareAndSet(0, 1)) {
            SerdesBinary.register(serdesId(), this);
            SerdesJson.register(serdesId(), this);
        }
    }
    
    private static final AtomicInteger registered = new AtomicInteger();
}
